package leetcode.editor.cn;
 
/**
 * 最小路径和
 * @author qulq
 * @date 2024-07-13 11:20:16
 */
public class T64_MinimumPathSum{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T64_MinimumPathSum().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int minPathSum(int[][] grid) {

		// 滚动数组优化，只开2行dp
		int row = grid.length, col = grid[0].length;
		int[][] dp = new int[grid.length][grid[0].length];
		dp[0][0] = grid[0][0];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (i == 0 && j == 0) {
					dp[i][j] = grid[i][j];
				} else if (i == 0) {
					dp[i][j] = dp[i][j - 1] + grid[i][j];
				} else if (j == 0) {
					dp[i][j] = dp[i - 1][j] + grid[i][j];
				} else {
					dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
				}
			}
		}
		return dp[row - 1][col - 1];
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}